package com.ztesoft.sca.controller;

import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.AudioSourceDto;
import com.ztesoft.sca.model.AudioSrcQo;
import com.ztesoft.sca.model.Response;
import com.ztesoft.sca.service.AudioSrcServiceImpl;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang.aozhao
 * @created 2018/5/23 20:28
 */
@RestController
@RequestMapping("/api/audio")
public class AudioSrcController {

    private static final Logger logger = LoggerFactory.getLogger(AudioSrcController.class);

    @Autowired
    AudioSrcServiceImpl audioSrcService;

    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    public String getAudioSrcById(@RequestParam(value = "fileid") Long fileId) {
        AudioSourceDto audioSourceDto;
        try {
            audioSourceDto = audioSrcService.getAudioSrcById(fileId);
        } catch (Exception e) {
            logger.error("根据id查询音频源异常", e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR, Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC, Constants.INF_DESC_SUCC, audioSourceDto);
    }

    @RequestMapping(value = "/list.do", method = {RequestMethod.POST})
    public String listAudioSrc(@RequestBody AudioSrcQo audioSrcQo) {
        if(logger.isDebugEnabled()){
            logger.debug("接受到的参数为： " + audioSrcQo.toString());
        }
        List<AudioSourceDto> audioSourceDtoList;
        try {
            audioSourceDtoList = audioSrcService.listAudioSrc(audioSrcQo);
        } catch (Exception e) {
            logger.error("查询音频源列表异常",e);
            return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
        }
        return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,audioSourceDtoList);
    }
}
